package com.bianguo.android.tencentpushdemo.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/12.
 */

public class HomeAdapter extends UltimateViewAdapter<HomeAdapter.ViewHolder> {

    private Context mContext;
    private List<String> list;
    private LayoutInflater inflater;
    private OnClickItemListener onClickItemListener;

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public HomeAdapter(Context mContext, List<String> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder newFooterHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder newHeaderHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_home_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getAdapterItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public long generateHeaderId(int position) {
//        if (getItem(position).length() > 0)
//            return getItem(position).charAt(0);
//        else return -1;
        String s = position + "";
        if (position < 10) {
            s = "0";
        }
        return s.charAt(0);
    }

    public String getItem(int position) {
        if (customHeaderView != null)
            position--;
        if (position >= 0 && position < list.size())
            return list.get(position);
        else return "";
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= list.size() : position < list.size()) &&
                (customHeaderView != null ? position > 0 : true)) {
            position -= customHeaderView == null ? 0 : 1;
            holder.text.setText(list.get(position));
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickItemListener != null) {
                        onClickItemListener.onClickItem(holder.text, holder.getAdapterPosition());
                    }
                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View headView = LayoutInflater.from(mContext).inflate(R.layout.head_home_layout, parent, false);
        return new RecyclerViewHolder(headView);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        RecyclerViewHolder viewHolder = (RecyclerViewHolder) holder;
        viewHolder.text.setText("headView" + position);
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        swapPositions(list, fromPosition, toPosition);
        super.onItemMove(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        removeInternal(list, position);
        super.onItemDismiss(position);
    }

    public static class ViewHolder extends UltimateRecyclerviewViewHolder {

        @BindView(R.id.item_home_text)
        TextView text;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.head_text)
        TextView text;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickItemListener{
        void onClickItem(View view, int position);
    }
}
